﻿// JScript 文件
//var mc = null;
//document.onload = function ()
//{
//    mc = new CtnMenuCtrl("mc",true,true,"menuctrl");
//}

//function ctmenu(evt)
//{
////    //mc.clear();
////    mc = new CtnMenuCtrl("mc");
////    with(mc)
////    {
////        addEvent("菜单一", "alert(1);");
////        with(addLink("链接一", "http://localhost/"))
////        {
////            addEvent("菜单一", "alert(1);");
////            addLink("链接一", "http://localhost/");
////        }
////        addEvent("菜单一", "alert(1);");
////        addLink("链接一", "http://localhost/");
////    }
////    mc.show(evt);
////    return false;
//}
//document.oncontextmenu = function (evt)
//{
//    mc.clear();
//    with(mc)
//    {
//        addEvent("菜单一", "alert(1);",null,"images/close.gif");
//        with(addLink("链接一", "http://localhost/"))
//        {
//            addEvent("菜单一", "alert(1);");
//            addLink("链接一", "http://localhost/");
//            with(addLink("链接一", "http://localhost/"))
//            {
//                addEvent("菜单一", "alert(1);");
//                addLink("链接一", "http://localhost/");
//                addSeparator();
//                addEvent("菜单一", "alert(1);");
//                addLink("链接一", "http://localhost/");
//            }
//            addEvent("菜单一", "alert(1);");
//            addLink("链接一", "http://localhost/");
//        }
//        addEvent("菜单一", "alert(1);");
//        with(addLink("链接一", "http://localhost/"))
//        {
//            addEvent("菜单一", "alert(1);");
//            addLink("链接一", "http://localhost/");
//            with(addLink("链接一", "http://localhost/"))
//            {
//                addEvent("菜单一", "alert(1);");
//                addLink("链接一", "http://localhost/");
//                addEvent("菜单一", "alert(1);");
//                addLink("链接一", "http://localhost/");
//            }
//            addEvent("菜单一", "alert(1);");
//            addLink("链接一", "http://localhost/");
//        }
//        addLink("链接一", "http://localhost/");
//    }
//    mc.show(evt);
//    return false;
//}
//document.onclick = function (evt)
//{
//    mc.hide();
//}

//document.onmouseup = function(evt)
//{
//}

function CtnMenuCtrl(name, bIcon, bRightMenu, css, fontsize)
{
    this.name = name;      //在文档中的变量名
    this.bIcon = bIcon == null ?false:bIcon;     //是否使用图片
    this.bRightMenu = bRightMenu == null ?true:bRightMenu;//是右键菜单还是下拉菜单
    this.css_div = css||"menuctrl";   //所使用样式
    this.fontsize = fontsize||13;     //字体s大小
    
    this.index = -1;     //在兄弟菜单项中的索引号
    this.id = "MenuCtrl";   //Id
    this.parent = null;   //父菜单
    this.text = null;       //链接名称
    this.jsEvent = null; //链接地址或Js
    this.title = null; //链接提示
    this.icon = null;       //链接图标
    
    this.element = null;    //菜单的层元素
    this.arrMenu = new Array(); //子菜单
    this.width = 60;        //菜单宽度
    this.css_tr_over = "mc_over";
    this.bOutHide = true;  // 当鼠标移出是菜单消失

    
    //接口
    this.addLink        = addLink_MC;
    this.addEvent       = addEvent_MC;
    this.addSeparator   = addSeparator_MC;
    this.show           = show_MC;
    this.clear          = clear_MC;
    this.useIcon        = useIcon_MC;
    this.outHide        = outHide_MC;
    this.setStyle       = setStyle_MC;
    
    //成员函数
    this.mouseover = mouseover_MC;
    this.mouseout = mouseout_MC;
    this.mouseleave = mouseleave_MC;
    this.mousein    = mousein_MC;
    this.isOut = isOut_MC;
    this.hide = hide_MC;
    this.visible = visible_MC;
    this.getHtml = getHtml_MC;
    this.getPosition = getPosition_MC;
    this.setAttribute = setAttribute_MC;
}

function addLink_MC(text, href, target, title, icon)
{
    var jsEvent = new String();
    if (href && href != "")
    {
        if (!target||target.toLowerCase() == "_self")
        {
            jsEvent = "window.location.href='" + href + "';";
        }
        else
        {
            jsEvent = "window.open('" + href + "','" + target + "');"
        }
    }
    return this.addEvent(text, jsEvent, title, icon);
}

function addSeparator_MC()
{
    var item = new CtnMenuCtrl(this.name + ".arrMenu[" + this.arrMenu.length + "]"
                            , this.arrMenu.length, this, null);
    this.arrMenu.push(item);
}

function show_MC(evt, srcElem)
{
    evt = evt||window.event;
    
    var elem = document.getElementById(this.id);
    if (elem && !this.element)
    {
        document.body.removeChild(elem);
        elem = null;
    }
    if (!elem)
    {
        elem = document.createElement("div");
        elem.id = this.id;
        elem.className = this.css_div;
        if (this.bOutHide)
        {
            var tmpObj = this;
            elem.onmouseout = function(evt)
            {
                tmpObj.mouseleave(evt);
            };
            elem.onmouseover = function(evt)
            {
                tmpObj.mousein(evt);
            };
        }
        document.body.appendChild(elem);
        elem.innerHTML = this.getHtml();
        elem.style.width = (elem.firstChild.offsetWidth + 3) + "px";
        elem.style.position = "absolute";
        elem.style.zIndex = "9000";
        this.element = elem;
    }
    //elem.style.display = "block";
    elem.style.visibility = "visible";
    
    var vLeft = 0;
    var vTop = 0;
    var docWidth = document.documentElement.clientWidth;//Math.max(document.documentElement.clientWidth, document.body.clientWidth);
    var docHeight = document.documentElement.clientHeight;//Math.max(document.documentElement.clientHeight, document.body.clientHeight);
    var scrollLeft = document.documentElement.scrollLeft;
    var scrollTop = document.documentElement.scrollTop;
    var vWidth = elem.offsetWidth;
    var vHeight = elem.offsetHeight;
    var srcWidth = 0;
    var srcHeight = 0;
    var pos = {x:evt.clientX, y:evt.clientY};
        
    if (this.parent || !this.bRightMenu)
    {
        srcWidth = srcElem.offsetWidth;
        srcHeight = srcElem.offsetHeight;
        pos = this.getPosition(srcElem);
    }
    
    if (!this.bRightMenu && !this.parent)
    {
        if (docWidth - pos.x < vWidth)
            vLeft = scrollLeft + pos.x + srcWidth - vWidth;
        else
            vLeft = scrollLeft + pos.x;

        if (docHeight - pos.y - srcHeight < vHeight)
        {
            vTop = scrollTop + pos.y - vHeight;
        }
        else
            vTop = scrollTop + pos.y;
    }
    else
    {
        if (docWidth - pos.x - srcWidth < vWidth)
            vLeft = scrollLeft + pos.x - vWidth;
        else
            vLeft = scrollLeft + pos.x + srcWidth;

        if (docHeight - pos.y < vHeight)
        {
            vTop = scrollTop + pos.y + srcHeight - vHeight;
        }
        else
            vTop = scrollTop + pos.y;
    }
    this.element.style.left = vLeft + "px";
    this.element.style.top = vTop + "px";
}

function addEvent_MC(text, jsEvent, title, icon)
{
    if (!text || text == "")
    {
        return null;
    }

//    var strlen = 0;
//    for (var i = 0; i < text.length; i++)
//    {
//        if (text.charCodeAt(i) < 256)
//        {
//            strlen += 2/3;
//        }
//        else
//        {
//            strlen++;
//        }
//    }
//    if ( this.width < this.fontsize * strlen + 6)
//    {
//        this.width = parseInt(this.fontsize * strlen) + 6;//6为space
//    }

    var item = new CtnMenuCtrl(this.name + ".arrMenu[" + this.arrMenu.length + "]"
                            , this.bIcon, this.bRightMenu, this.css_div, this.fontsize);
    item.setAttribute(this.arrMenu.length, this, text, jsEvent, title, icon);

    this.arrMenu.push(item);
    return item;
}

function getHtml_MC()
{
    var vHtml = new Array();
//    if (this.bIcon)
//    {
//        this.width = this.width + 18 + 6;
//    }
//    else
//    {
//        this.width = this.width + 6;
//    }
    vHtml.push('<table cellpadding="0" cellspacing="0">');// style="width:' + this.width + 'px">');
    for (var i = 0; i < this.arrMenu.length; i++)
    {
        if (!this.arrMenu[i].text)//分隔符号
        {
            if (this.bIcon)
            {
                vHtml.push('<tr><td id="' + this.id + '_tr_' + i + '" colspan="3">');
            }
            else
            {
                vHtml.push('<tr><td id="' + this.id + '_tr_' + i + '" colspan="2">');
            }
            vHtml.push('<hr/>');
            vHtml.push('</td></tr>');
        }
        else
        {
            vHtml.push('<tr id="' + this.id + '_tr_' + i + '"');
            vHtml.push(' title="' + this.arrMenu[i].title + '"');
            vHtml.push(' onclick="' + this.name + '.hide(true);' + this.arrMenu[i].jsEvent + ';"');
            vHtml.push(' onmouseover="' + this.name + '.mouseover(this, event, ' + i + ');"');
            vHtml.push(' onmouseout="' + this.name + '.mouseout(this, event, ' + i + ');"');
            vHtml.push('>');
            if (this.bIcon)
            {
                vHtml.push('<td><img src="' + this.arrMenu[i].icon + '" alt=""/></td>');
            }
            vHtml.push('<td>');
            vHtml.push(this.arrMenu[i].text);
            vHtml.push('</td><td>');
            if (this.arrMenu[i].arrMenu.length > 0)
            {
                vHtml.push('&gt;');
            }
            else
            {
                vHtml.push('&nbsp;');
            }
            vHtml.push('</td></tr>');
        }
    }
    vHtml.push('</table>');
    //vHtml += '</div>';
    return vHtml.join("");
}

function mouseout_MC(elem, evt, idx)
{
    if(!this.isOut(elem, evt))
    {
        return null;
    }
    elem.className = "";
}

function mouseover_MC(elem, evt, idx)
{
    elem.className = this.css_tr_over;
    if (this.parent)
    {
        document.getElementById(this.parent.id + '_tr_' + this.index).className = this.parent.css_tr_over;
    }
    
    for (var i = 0; i < this.arrMenu.length; i++)
    {
        if (i != idx)
        {
            this.arrMenu[i].hide();
            document.getElementById(this.id + '_tr_' + i).className = "";
        }
    }
    
    if (this.arrMenu[idx].arrMenu.length > 0)
    {
        this.arrMenu[idx].show(evt, elem);
    }
}
function mouseleave_MC(evt)
{
    if(!this.isOut(this.element, evt))
    {
        return null;
    }
    this.hide(true);
}

function isOut_MC(elem, evt)
{
    evt = evt||window.event;
    /* FF 下判断鼠标是否离开DIV */
    if(window.navigator.userAgent.indexOf("Firefox")>=1)
    {
        var x = evt.clientX + document.documentElement.scrollLeft;
        var y = evt.clientY + document.documentElement.scrollTop ;
        var vLeft = elem.offsetLeft;
        var vTop = elem.offsetTop;
        var w = elem.offsetWidth;
        var h = elem.offsetHeight;
       
        if(y < vTop || y > (h + vTop) || x > vLeft + w || x<vLeft )
        {
            return true;
        }
    }

    /* IE */
	 if(window.navigator.userAgent.indexOf("MSIE")>=1) 
	 {
			if(elem.contains(evt.toElement ) == false )
			{
				return true;
			}
	}
	return false;
}

function useIcon_MC(flag)
{
    if (flag == null)
    {
        this.bIcon = true;
    }
    else
    {
        this.bIcon = flag;
    }
}

function hide_MC(flag)
{
    if (flag)
    {
        var tmp = this;
        while (tmp.parent)
        {
            tmp= tmp.parent;
        }
        tmp.hide();
        return;
    }
    
    if (this.element)
    {
        //this.element.style.display = "none";
        this.element.style.visibility = "hidden";
    }
    for (var i = 0; i < this.arrMenu.length; i++)
    {
        this.arrMenu[i].hide();
    }

}

function clear_MC()
{
    this.hide();
    this.arrMenu = new Array();
}

Number.prototype.NaN0=function(){return isNaN(this)?0:this;}
function getPosition_MC(e)
{
    var left = 0;
    var top  = 0;
    while (e.offsetParent)
    {
        left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
        top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
        e     = e.offsetParent;
    }
    left += e.offsetLeft + (e.currentStyle?(parseInt(e.currentStyle.borderLeftWidth)).NaN0():0);
    top  += e.offsetTop  + (e.currentStyle?(parseInt(e.currentStyle.borderTopWidth)).NaN0():0);
    return {x:left, y:top};
}

function setAttribute_MC(index, parent, text, jsEvent, title, icon)
{
    this.index = index;     //在兄弟菜单项中的索引号
    this.id = parent.id + "_" + index;   //Id
    this.parent = parent;   //父菜单
    this.text = text;       //链接名称
    this.jsEvent = jsEvent; //链接地址或Js
    this.title = title||""; //链接提示
    this.icon = icon;       //链接图标
}

function setStyle_MC(css)
{
    this.css_div = css;
}


function mousein_MC(evt)
{
    this.visible();
}

function visible_MC()
{
    if (this.element)
    {
        this.element.visibility = "visible";
    }
    var tmp = this.parent;
    while(tmp)
    {
        this.element.visibility = "visible";
        tmp = tmp.parent;
    }
}

function outHide_MC(flag)
{
    this.bOutHide = flag == null?true:flag;
}